using System;
using System.Collections.Generic;
using UnityEngine;

public class UIPlayerScoreBoard : MonoBehaviour
{
	[Serializable]
	public class ColourRangesForTimer
	{
		public FloatRange fractionForColour;

		public Color colour;
	}

	public UISprite flag;

	public new UILabel name;

	public UILabel score;

	public UITexture texture;

	public UILabel timer;

	public UISprite topLayer;

	public UISprite bck;

	public GameObject star;

	public UILabel starLabel;

	public UILabel sets;

	public MessageDisplay messageDisplay;

	public List<ColourRangesForTimer> colourRanges = new List<ColourRangesForTimer>();

	public Color defaultTimerColour;

	private GGServerRequestsBackend.TextureRequest textureRequest;

	public void Awake()
	{
		topLayer.color = GetColourForTimeFraction(1f);
		topLayer.alpha = 1f;
		topLayer.fillAmount = 1f;
	}

	private Color GetColourForTimeFraction(float fraction)
	{
		float value = Mathf.Clamp01(fraction);
		foreach (ColourRangesForTimer colourRange in colourRanges)
		{
			if (colourRange.fractionForColour.isIncluding(value))
			{
				return colourRange.colour;
			}
		}
		return defaultTimerColour;
	}

	public void SetSets(int sets)
	{
		this.sets.text = sets.ToString();
	}

	public void ShowStar(int level)
	{
		UITools.ChangeText(starLabel, level.ToString());
		if (star != null)
		{
			star.SetActive(true);
		}
	}

	public void HideStar()
	{
		if (star != null)
		{
			star.SetActive(false);
		}
	}

	public void SetNameFlagAndProfilePicture(string nameTxt, int flagIndex, string profileUrl, bool isUser)
	{
		name.text = GGFormat.FirstName(nameTxt).ToUpper();
		UITools.SetActive(texture, false);
		flag.gameObject.SetActive(true);
		flag.spriteName = GameConstants.Instance.CountryForFlag(flagIndex).spriteName;
		if (GGPlayerSettings.instance.useProfilePictureInMultiplayer && profileUrl != string.Empty)
		{
			textureRequest = new GGServerRequestsBackend.TextureRequest(profileUrl);
			textureRequest.cache = ((!isUser) ? CacheStategy.GetFromCache : CacheStategy.CacheToFile);
			textureRequest.onComplete = OnRequestComplete;
			BehaviourSingleton<GGRequestLimitter>.instance.Add(textureRequest);
		}
	}

	public void OnRequestComplete(GGServerRequestsBackend.ServerRequest request)
	{
		GGServerRequestsBackend.TextureRequest textureRequest = request as GGServerRequestsBackend.TextureRequest;
		if (textureRequest != null && textureRequest == this.textureRequest)
		{
			if (request.status != 0)
			{
				this.textureRequest = null;
				return;
			}
			texture.gameObject.SetActive(true);
			flag.gameObject.SetActive(false);
			texture.mainTexture = textureRequest.response;
		}
	}

	public void SetColour(Color col)
	{
		topLayer.color = col;
	}

	public void SetActive(bool active)
	{
		topLayer.gameObject.SetActive(true);
		topLayer.fillAmount = 1f;
		topLayer.color = GetColourForTimeFraction(1f);
		topLayer.alpha = ((!active) ? 1 : 0);
		TweenAlpha.Begin(topLayer.cachedGameObject, 0.25f, active ? 1 : 0);
		bck.cachedGameObject.SetActive(active);
	}

	public void HideTime()
	{
		topLayer.fillAmount = 0f;
		topLayer.color = GetColourForTimeFraction(1f);
	}

	public void SetTime(float time, float totalTime)
	{
		float num = Mathf.Clamp01(time / totalTime);
		topLayer.gameObject.SetActive(true);
		topLayer.fillAmount = num;
		topLayer.color = GetColourForTimeFraction(num);
	}

	public void SetScore(string score)
	{
		UITools.ChangeText(this.score, score);
	}
}
